package com.test.lol;

import android.app.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import com.google.gson.Gson;
import java.io.*;
import java.util.*;
import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.entity.*;
import org.apache.http.impl.client.*;


public class CommentListViewActivity  extends Activity{

	Bundle extra;
	DBResponseObject dbResponse;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.commentlistview);
    	ListView lstTest;
    	
    	lstTest = (ListView) findViewById(R.id.lstV);
    	
		try {
			Log.v(this.getClass().getName(), "CommentListViewActiv Started");
			extra = getIntent().getExtras();
				
			
			if(extra == null){
				return;
			}
			Gson g = new Gson();
			CommentObject[] commentObj = g.fromJson(extra.getString("Comments"), CommentObject[].class);
			ArrayList<CommentObject> commentList = new ArrayList<CommentObject>();
			
			
			for(int i = 0; i < commentObj.length; i++){
				commentList.add(commentObj[i]);
			}
	

			lstTest.setAdapter(new CommentArrayAdapter(this, R.layout.list_item, commentList));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e(this.getClass().getName(), "ERROOOOOOOOOOR I COMMENTLISTVIOEWACTIVIT "+ e.getMessage());
		}
		
	}
    
    public void onSend(View view){
    	try{
    		Log.v(this.getClass().getName(), "CommentActivity Photo ID: " + extra.getString("ID"));
    		EditText name = (EditText) findViewById(R.id.NameCaptionInput);
	    	EditText comment = (EditText) findViewById(R.id.otherCaptionInput);
	    	String sendStr = "{'name': '"+name.getText()+"', 'comment': '" +comment.getText()+"'}";
	    	StringEntity entity = new StringEntity(sendStr);
	    	
	    	Log.v(this.getClass().getName(), "CommentActivity Send String: " +sendStr);
	    	
	    	HttpClient client;
	    	HttpPost post;
	    	HttpResponse response;	     
	    	
	    	post = new HttpPost(Common.baseUrl+extra.getString("ID")+"/comments/");    	   	
	        client = new DefaultHttpClient();
	        entity.setContentType("application/json");
	        post.setEntity(entity);      
        	response = client.execute(post); 
        	
		    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		    String line;
		    StringBuilder total = new StringBuilder();
		    
		    // Read response until the end
		    while ((line = rd.readLine()) != null) { 
		        total.append(line);
		        Log.v(this.getClass().getName(), "Response: " + line);
		    }
        	
        	Gson g = new Gson();
        	
        	dbResponse = g.fromJson(response.toString(), DBResponseObject.class);

        	Log.v(this.getClass().getName(), "Response: " +dbResponse.getMessage());
        	
        }catch (Exception e){
        	Log.e(this.getClass().getName(), "Post exception: "+ dbResponse.getMessage());
        }
    }	
}

